package org.example.dao.impl;

import org.example.dao.FullDataMapper;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class DBUtil {

    static Properties properties=new Properties();
    static String url;
    static String username;
    static String password;

    static {
        InputStream is=DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
        try{
            properties.load(is);
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e){
            System.out.println("出现异常"+e.getMessage());
        }
    }

    public static Connection getConn(){
        try{
            return DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            System.out.println("连接异常");
            return null;
        }
    }

    public static void close(Connection conn, PreparedStatement ps, ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(conn!=null){
                conn.close();
            }
        } catch (SQLException e) {
            System.out.println("关闭资源异常");
        }
    }

    public static int executeSql(String sql,Object ... params) {
        Connection conn = getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            for(int i = 0;i < params.length;i++) {
                ps.setObject(i + 1,params[i]);
            }
            return ps.executeUpdate();
        }catch (SQLException e) {
            System.out.println("执行sql语句失败");
        } finally {
            close(conn,ps,null);
        }
        return 0;
    }


    public static <T> List<T> executeQuery(FullDataMapper<T> mapper, String sql, Object ... params ) {
        List<T> list = new ArrayList<>();
        Connection conn = getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for(int i = 0;i < params.length;i++) {
                ps.setObject(i+1,params[i]);
            }
            rs = ps.executeQuery();
            while(rs.next()) {
                list.add(mapper.map(rs));
            }
        } catch(SQLException ex) {
            ex.printStackTrace();
        } finally {
            close(conn,ps,rs);
        }
        return list;
    }

    public  static int excuteSql1(String sql,Object...params) {
        Connection conn = getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("执行sql语句失败");
            e.printStackTrace();
        } finally {
            close(conn, ps, null);
        }
        return 0;
    }
}